import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.UIManager;
import java.util.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
import java.io.File;
import java.io.FileNotFoundException;

public class Days extends JPanel{

        public JPanel notesPanel = new JPanel();
        public JTextArea notesArea = new JTextArea(4,26);
        public JButton addNotesB = new JButton("Save");
        public JButton importB = new JButton("Import");
        public JButton exportB = new JButton("Export");

        public JPanel clockPanel = new JPanel();
        public JTextField clockField = new JTextField(7);

        public JPanel alarmClockPanel = new JPanel();
        String alarmHour[] = {"AM","PM"};
        public JComboBox alarmHour2 = new JComboBox(alarmHour);
        public JButton alarmOnB = new JButton("On");
        public JButton alarmOffB = new JButton("Off");
        public JComboBox hour;
        public JComboBox min;
        public JComboBox sec;
        
        public String strAlarmSound[] = {"alarm1", "alarm2", "alarm3", "alarm4"};
        public JComboBox alarmSounds;
        
        public String strHr[]= {"00","01","02","03","04","05","06","07","08",
                "09","10","11","12"};
        public String strMin[]= {"00","01","02","03","04","05","06","07","08",
                "09","10","11","12","13","14","15","16","17","18","19","20",
                "21","22","23","24","25","26","27","28","29","30","31","32",
                "33","34","35","36","37","38","39","40","41","42","43","44",
                "45","46","47","48","49","50","51","52","53","54","55","56",
                "57","58","59",};
        public String strSec[]= {"00","01","02","03","04","05","06","07","08",
                "09","10","11","12","13","14","15","16","17","18","19","20",
                "21","22","23","24","25","26","27","28","29","30","31","32",
                "33","34","35","36","37","38","39","40","41","42","43","44",
                "45","46","47","48","49","50","51","52","53","54","55","56",
                "57","58","59"};

        public JLabel alarmStatus;
        public JLabel hourL;

        public JLabel sun;
        public JLabel mon;
        public JLabel tue;
        public JLabel wed;
        public JLabel thu;
        public JLabel fri;
        public JLabel sat;

        public Icon pic[]= {new ImageIcon(getClass()
                .getResource("blackArrowUp.gif")),new ImageIcon(getClass()
                        .getResource("blackArrowDown.gif"))};

        public JPanel mainpanel;   		//for add days and weekday
        public JPanel p[]= new JPanel[42];	//Array of Panels for Adding Days Buttons
        public JButton b[]= new JButton[31];	//Array of 31 Buttons For 31 Days

	//for year , month and date (all will be added to First )

        public JPanel datepanel;		//Panel on which date , year and month will add
        public JTextField date;			//for showing date in text fields
        public JComboBox month;			//for months
        public JTextField year;			//for year
        public JButton sb[]= new JButton[2];	// for increasing year by 1 on every click

	//string array has name of months for adding to combobox

        public String strmon[]= {"January","Febuary","March","April","May",
                "June","July","Augest","September","October","November",
                "December"};

	//Constructor Start
        Days(){
                setLayout(null);		//setting layout
                setSize(350,228);
                setVisible(true);
                
                alarmSounds = new JComboBox(strAlarmSound); // combo box for alarm clip

                //first Frame Build

                datepanel = new JPanel();
                datepanel.setLayout(null);
                //datepanel.setLayout(new GridLayout(1,4,2,2));
                datepanel.setSize(325,31);
                datepanel.setLocation(1,0);

                date = new JTextField(); 	//date textField will strore the date
                date.setFocusable(false);
                date.setSize(100,30);
                date.setLocation(5,0);
                date.setBackground(Color.WHITE);
                date.setFont(new Font("Arial",Font.PLAIN,14));
                date.setEditable(false);

                month = new JComboBox(strmon);			//month combo box will have all months
                month.setFocusable(false);
                month.setSize(90,30);
                month.setLocation(110,0);

                year = new JTextField("2008");		//year will have year part of date
                year.setFocusable(false);
                year.setSize(70,30);
                year.setLocation(205,0);
                year.setBackground(Color.WHITE);
                year.setEditable(false);

                sb[0] = new JButton("");				//sb button increments the year by 1
                sb[0].setFocusable(false);
                sb[0].setSize(30,15);
                sb[0].setLocation(275,0);
                sb[0].setIcon(pic[0]);

                sb[1] = new JButton("");				//sb button decrements the year by 1
                sb[1].setFocusable(false);
                sb[1].setSize(30,15);
                sb[1].setLocation(275,16);
                sb[1].setIcon(pic[1]);

                datepanel.add(date);
                datepanel.add(month);
                datepanel.add(year);
                datepanel.add(sb[0]);
                datepanel.add(sb[1]);
                add(datepanel);



	//Start of Notes
	//notesPanel.setBackground(Color.GRAY);
	//notesPanel.setSize(315,95);
	notesPanel.setSize(315,195);
	notesPanel.setLocation(1,300);
	notesArea.setLocation(5,5);
	notesArea.setLineWrap(true);
        notesArea.setWrapStyleWord(true);
      	addNotesB.setLocation(300,80);
      	addNotesB.setBorderPainted(false);
        addNotesB.setFocusPainted(false);
        importB.setBorderPainted(false);
        importB.setFocusPainted(false);
        exportB.setBorderPainted(false);
        exportB.setFocusPainted(false);
	notesPanel.add(new JScrollPane(notesArea));
	notesPanel.add(addNotesB);
	notesPanel.add(importB);
	notesPanel.add(exportB);
	
      	notesPanel.setVisible(true);
	add(notesPanel);
	//End of Notes

		//Start of Clock
	clockPanel.setLayout(new FlowLayout());
	clockPanel.setSize(150,80);
	clockPanel.setLocation(6,215);
	clockField.setFocusable(false);
	clockPanel.add(clockField);
	clockPanel.add(alarmSounds);
	clockField.setEditable(false);
        clockField.setFont(new Font("sansserif", Font.PLAIN, 24));
		//clockField.setText("00:00:00");
	add(clockPanel);
	javax.swing.Timer t = new javax.swing.Timer(1000,
		new ClockListener());
	t.start();
		//End of Clock

		//Start of AlarmClock
	hour = new JComboBox(strHr);
	hour.setFocusable(false);
	hour.setSize(90,30);
	hour.setLocation(1,1);
	min = new JComboBox(strMin);
	min.setFocusable(false);
	min.setSize(90,30);
	min.setLocation(1,1);
	sec = new JComboBox(strSec);
	sec.setFocusable(false);
	sec.setSize(90,30);
	sec.setLocation(1,1);

	alarmClockPanel.setSize(160,105);
	alarmClockPanel.setLocation(155,195);
		//alarmClockPanel.setBackground(Color.GRAY);
	alarmOffB.setLocation(1,1);
	alarmOnB.setLocation(1,1);
	alarmHour2.setFocusable(false);
      	alarmOffB.setFocusPainted(false);
      	alarmOffB.setEnabled(false);
      	alarmOnB.setFocusPainted(false);
      	alarmClockPanel.add(hour);
      	alarmClockPanel.add(min);
      	alarmClockPanel.add(sec);
      	hourL = new JLabel("Hours | Minutes | Seconds");
      	alarmClockPanel.add(hourL);
      	alarmClockPanel.add(alarmHour2);
      	alarmClockPanel.add(alarmOnB);
      	alarmClockPanel.add(alarmOffB);
      	alarmStatus = new JLabel("Alarm Clock is Off");
      	alarmClockPanel.add(alarmStatus);
	add(alarmClockPanel);

		//End of AlarmClock

		//Days Frame

	mainpanel = new JPanel();
	mainpanel.setLayout(new GridLayout(7,7,1,1));
	mainpanel.setSize(315,160);
	mainpanel.setLocation(1,31);

		//Calendar now = Calendar.getInstance();
		//int y=getYear();
		//System.out.println(now.get(Calendar.DATE));
		//System.out.println('01-02-2010'.get(Calendar.DAY_OF_MONTH));
		//System.out.println(now.get(Calendar.DAY_OF_WEEK));
		//System.out.println(now.get(Calendar.YEAR));
		//System.out.println(dayOfWeek);

	sun = new JLabel("Sun");
	sun.setForeground(Color.RED);
	mon = new JLabel("Mon");
	tue = new JLabel("Tue");
	wed = new JLabel("Wed");
	thu = new JLabel("Thu");
	fri = new JLabel("Fri");
	sat = new JLabel("Sat");

		//add labels to panel
	mainpanel.add(sun);
	mainpanel.add(mon);
	mainpanel.add(tue);
	mainpanel.add(wed);
	mainpanel.add(thu);
	mainpanel.add(fri);
	mainpanel.add(sat);

		//Initializing memory to Jpanels and add it to mainpanel
	for (int x=0;x<42;x++){
		p[x]= new JPanel();
		p[x].setLayout(new GridLayout(1,1));
		p[x].setBackground(Color.WHITE);
		mainpanel.add(p[x]);
		validate();
	}

	final HandlerClass handler= new HandlerClass();		// object of handlerclass

	for (int i=0;i<31;i++){			//only Initializing memory to buttons not adding them
		b[i]= new JButton();
		b[i].addActionListener(handler);
		b[i].setFont(new Font("Times New Roman",Font.PLAIN,11));
		b[i].setFocusable(false);
		b[i].setText(Integer.toString(i+1));
		}


	final Calendar now = GregorianCalendar.getInstance(); 		//create a Calendar object
	year.setText(Integer.toString(now.get(Calendar.YEAR)));		//get year and month from Calendar object
	month.setSelectedIndex(now.get(Calendar.MONTH));			//add values to year and month

	validate();

		// DAY_OF_WEEK GIVES THE DAY OF THE FROM WIHCH THE MONTH STARTS
	int ye=Integer.parseInt(year.getText());
	Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

	int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

	for (int i=0;i<getDaysNo();i++){
		p[dayOfWeek-1].add(b[i]);
		dayOfWeek++;
	}





	add(mainpanel);						// add main pannel to

	validate();

	month.addItemListener(
	new ItemListener(){

	public void itemStateChanged(ItemEvent event){
	        if (event.getStateChange()==ItemEvent.SELECTED){
			mainpanel.removeAll();
			validate();

			mainpanel.add(sun);
			mainpanel.add(mon);
			mainpanel.add(tue);
			mainpanel.add(wed);
			mainpanel.add(thu);
			mainpanel.add(fri);
			mainpanel.add(sat);

			for (int x=0;x<42;x++){
				p[x].removeAll();
				mainpanel.add(p[x]);
				validate();
			}


			int ye=Integer.parseInt(year.getText());
			Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);



			int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

			for (int i=0;i<getDaysNo();i++){
				p[dayOfWeek-1].add(b[i]);
				dayOfWeek++;
				validate();
			}

			validate();
		}



		datepanel.validate();
		validate();

		String date2 = Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText());
		date.setText(date2);
		setNotes(date2);
		setDateColor();
	}
	}
	);


	sb[0].addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int y = Integer.parseInt(year.getText());
				y++;
				onYearClick(y);
						//sets the date on Click of year Button
				String date2 = Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText());
				date.setText(date2);
				setNotes(date2);
				setDateColor();
			}
		}
	);



	sb[1].addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int y = Integer.parseInt(year.getText());
				y--;
				onYearClick(y);
					//sets the date on Click of year Button
				String date2 = Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText());
				date.setText(date2);
				setNotes(date2);
				setDateColor();
			}

		}
	);

	addNotesB.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Calendar_Methods cm = new Calendar_Methods();
				cm.saveNotes(date.getText(),notesArea.getText());
			}

		}
	);
	
	importB.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				try{
					JFileChooser chooser = new JFileChooser();
               		chooser.setCurrentDirectory(new File("."));
					chooser.setDialogTitle("Choose Directory");
					chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					  Calendar_Methods cm = new Calendar_Methods();
					  String path = chooser.getSelectedFile().getAbsolutePath();
					  String fullPath = path + "\\notes.cnf";
					  System.out.println(fullPath);
					  cm.copyAFile(fullPath, "notes.cnf");
					  String date2 = Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText());
					  setNotes(date2);
					} else {
					  System.out.println("No Selection ");
					}
				}catch(Exception exep){
					
				}
				
			}
		}
	);
	
	exportB.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				try{
					JFileChooser chooser = new JFileChooser();
					chooser.setCurrentDirectory(new java.io.File("."));
					chooser.setDialogTitle("Choose notes.cnf");
					chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					  Calendar_Methods cm = new Calendar_Methods();
					  cm.copyAFile("notes.cnf",chooser.getSelectedFile().getAbsolutePath().concat("\\notes.cnf"));
					} else {
					  System.out.println("No Selection ");
					}
				}catch(Exception exep){
					
				}
				
			}
		}
	);

	alarmOnB.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Calendar_Methods cm = new Calendar_Methods();
				cm.setAlarm(String.valueOf(hour.getSelectedItem()),String.valueOf(min.getSelectedItem()),String.valueOf(sec.getSelectedItem()),String.valueOf(alarmHour2.getSelectedItem()),String.valueOf(alarmSounds.getSelectedItem()));
				hour.setEnabled(false);
				min.setEnabled(false);
				sec.setEnabled(false);
				alarmHour2.setEnabled(false);
				alarmOnB.setEnabled(false);
				alarmOffB.setEnabled(true);
				checkIfAlarmed();
			}

		}
	);

	alarmOffB.addActionListener(
		new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Calendar_Methods cm = new Calendar_Methods();
				cm.removeAlarm();
				hour.setEnabled(true);
				min.setEnabled(true);
				sec.setEnabled(true);
				alarmHour2.setEnabled(true);
				alarmOnB.setEnabled(true);
				alarmOffB.setEnabled(false);
				checkIfAlarmed();
			}

		}
	);

		//sets the date on form load
	setDateColor();
	String date2 = Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText());
	setNotes(date2);
	date.setText(date2);
	checkIfAlarmed();
	}


	//runs when year changes
	public void onYearClick(int y){
		year.setText(Integer.toString(y));

		mainpanel.removeAll();
		mainpanel.setBackground(Color.RED);
		mainpanel.setBackground(Color.BLUE);
		validate();

		mainpanel.add(sun);
		mainpanel.add(mon);
		mainpanel.add(tue);
		mainpanel.add(wed);
		mainpanel.add(thu);
		mainpanel.add(fri);
		mainpanel.add(sat);


		for (int x=0;x<42;x++){
			p[x].removeAll();
			mainpanel.add(p[x]);
			//mainpanel.setBackground(Color.RED);
			validate();
		}

		int ye=Integer.parseInt(year.getText());
		Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

		for (int i=0;i<getDaysNo();i++){

			p[dayOfWeek-1].add(b[i]);
			dayOfWeek++;
			validate();
			setDateColor();
		}

		validate();


	}



	//returns no. of days in a week
	public int getDaysNo(){
		int no = 31;
		if (month.getSelectedIndex()==1){
			no=28;
			if (Integer.parseInt(year.getText())%4==0){
				no=29;

			}
			month.validate();
		}

		if (month.getSelectedIndex()==3 || month.getSelectedIndex()==5 || month.getSelectedIndex()==8 || month.getSelectedIndex()==10)
		{
			no=30;
		}
		return no;

	}


	// handler class set which date is selected

	class HandlerClass implements ActionListener{


		Calendar now = Calendar.getInstance();
		public int db=(now.get(Calendar.DATE))-1;

		public void actionPerformed(ActionEvent e){
			for (int k=0;k<31;k++){

			 		if (e.getSource()==b[k]){
			 			//b[k].setEnabled(false);
			 			validate();
			 			db=k;
			 		}
			 		else{
			 			b[k].setEnabled(true);
			 			validate();
			 			int bd=now.get(Calendar.DATE);				//getting the current date
						setDateColor();
			 		}
			 		month.validate();

			 	}

				String date2 = Integer.toString(db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex()+1)).concat(" / ").concat(year.getText());
			 	date.setText(date2);
			 	setNotes(date2);
		}


	}

	public void setNotes(String date2){
		Calendar_Methods cm = new Calendar_Methods();
		notesArea.setText(cm.getNotes(date2));
	}

	public void checkIfAlarmed(){
		Calendar_Methods cm = new Calendar_Methods();
		String[] alarm = cm.checkAlarm();
			hour.setSelectedItem(alarm[0]);
			min.setSelectedItem(alarm[1]);
			sec.setSelectedItem(alarm[2]);
			alarmHour2.setSelectedItem(alarm[5]);
			alarmSounds.setSelectedItem(alarm[4]);
			if(!alarm[3].equals("Off")){
				hour.setEnabled(false);
				min.setEnabled(false);
				sec.setEnabled(false);
				alarmHour2.setEnabled(false);
				alarmOnB.setEnabled(false);
				alarmOffB.setEnabled(true);
				alarmSounds.setEnabled(false);
				alarmStatus.setText("Alarm Clock is ON");
			}else{
				alarmSounds.setEnabled(true);
				hour.setEnabled(true);
				min.setEnabled(true);
				sec.setEnabled(true);
				alarmOnB.setEnabled(true);
				alarmOffB.setEnabled(false);
				alarmStatus.setText("Alarm Clock is Off");
			}

	}
	public void setDateColor(){
		Calendar now = Calendar.getInstance();
		int bd=now.get(Calendar.DATE);		//getting the current date
		int bd2 = now.get(Calendar.MONTH);
		int bd4 = now.get(Calendar.YEAR);
		String bd5 = Integer.toString(bd4);
		String bd3 = strmon[bd2];
		if((bd3.equals(month.getSelectedItem())) && (bd5.equalsIgnoreCase(year.getText()))){
			b[bd-1].setForeground(Color.BLUE);					// sets the current date button color
		}else{
			b[bd-1].setForeground(Color.BLACK);
		}
	}

	class ClockListener implements ActionListener {

		        public void actionPerformed(ActionEvent e) {

		            Calendar now = Calendar.getInstance();

		            int h = now.get(Calendar.HOUR_OF_DAY);
		            int m = now.get(Calendar.MINUTE);
		            int s = now.get(Calendar.SECOND);

		            //int tempH = h % 12;
		            int tempH = h;
		            String alarmHourType = "AM";
		            if(h == 0){
		            	tempH = 12;
		            }
		            if(h > 12 && h!=0){
		            	tempH = h-12;

		            	alarmHourType = "PM";
		            }
		            if(h == 12 && s > 0){
		            	alarmHourType = "PM";
		            }



		            String h2 = Integer.toString(tempH);
		            String m2 = Integer.toString(m);
		            String s2 = Integer.toString(s);
		            if(tempH < 10){
		            	h2 = "0" + h2;
		            }
		            if(m < 10){
		            	m2 = "0" + m2;
		            }
		            if(s < 10){
		            	s2 = "0" + s2;
		            }
					String time = h2 + ":" + m2 + ":" + s2 + ":" + alarmHourType;
					Calendar_Methods cm = new Calendar_Methods();
		            clockField.setText(time);
					if(cm.checkIfAlarm(time)){
						cm.triggerAlarm();
					}
		        }

		    }

}

